var express = require('express');
var router = express.Router();
const { UserModel } = require('../model/module');

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

// 用户注册接口
router.post('/register', async (req, res) => {
  try {
    const { username, password, phone, youxiang } = req.body;

    // 验证必填字段
    if (!username || !password || !phone || !youxiang) {
      return res.status(400).json({
        code: 400,
        message: '请填写所有必填字段'
      });
    }

    // 检查用户名是否已存在
    const existingUser = await UserModel.findOne({ username });
    if (existingUser) {
      return res.status(400).json({
        code: 400,
        message: '用户名已存在'
      });
    }

    // 创建新用户
    const newUser = new UserModel({
      username,
      password,
      phone,
      youxiang,
      money: 0 // 明确设置初始余额为0
    });

    // 保存用户到数据库
    const savedUser = await newUser.save();

    res.status(200).json({
      code: 200,
      message: '注册成功',
      data: {
        username: savedUser.username,
        phone: savedUser.phone,
        youxiang: savedUser.youxiang,
        money: savedUser.money
      }
    });
  } catch (error) {
    console.error('注册失败:', error);
    res.status(500).json({
      code: 500,
      message: '服务器错误，注册失败'
    });
  }
});

// 检查用户名是否存在
router.post('/check-username', async (req, res) => {
  try {
    const { username } = req.body;
    
    if (!username) {
      return res.status(400).json({
        code: 400,
        message: '请提供用户名'
      });
    }

    const user = await UserModel.findOne({ username });
    
    res.status(200).json({
      code: 200,
      exists: !!user
    });
  } catch (error) {
    console.error('检查用户名失败:', error);
    res.status(500).json({
      code: 500,
      message: '服务器错误'
    });
  }
});

// 重置密码
router.post('/reset-password', async (req, res) => {
  try {
    const { username, newPassword } = req.body;
    
    if (!username || !newPassword) {
      return res.status(400).json({
        code: 400,
        message: '请提供用户名和新密码'
      });
    }

    const user = await UserModel.findOne({ username });
    
    if (!user) {
      return res.status(404).json({
        code: 404,
        message: '用户不存在'
      });
    }

    // 更新密码
    user.password = newPassword;
    await user.save();

    res.status(200).json({
      code: 200,
      message: '密码重置成功'
    });
  } catch (error) {
    console.error('重置密码失败:', error);
    res.status(500).json({
      code: 500,
      message: '服务器错误'
    });
  }
});

// 获取用户信息
router.post('/get-user-info', async (req, res) => {
  try {
    const { username } = req.body;
    
    if (!username) {
      return res.status(400).json({
        code: 400,
        message: '请提供用户名'
      });
    }

    const user = await UserModel.findOne({ username });
    
    if (!user) {
      return res.status(404).json({
        code: 404,
        message: '用户不存在'
      });
    }

    res.status(200).json({
      code: 200,
      data: {
        username: user.username,
        money: user.money
      }
    });
  } catch (error) {
    console.error('获取用户信息失败:', error);
    res.status(500).json({
      code: 500,
      message: '服务器错误'
    });
  }
});

module.exports = router;
